package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.common.domain.AjaxResult;
import com.itheima.common.domain.PageInfo;
import com.itheima.user.entity.UmsAdmin;
import com.itheima.user.entity.UmsRole;
import com.itheima.service.IUmsAdminService;
import com.itheima.vo.UmsAdminRoleRelationVo;
import com.itheima.vo.UpdateAdminPasswordParamVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/admin")
public class UmsAdminController {

    @Autowired
    private IUmsAdminService iUmsAdminService;

    /**
     * 登录以后返回token
     *
     * @param umsAdmin
     * @return
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody UmsAdmin umsAdmin) {
        log.info("用户{}正在登录", umsAdmin.getUsername());
        return iUmsAdminService.login(umsAdmin);
    }

    /**
     * 获取当前登录用户信息
     *
     * @return
     */
    @GetMapping("/info")
    public AjaxResult info(HttpServletRequest request) {
        Map<String,Object> info = iUmsAdminService.info(request);
        return AjaxResult.success("操作成功",info);
    }

    /**
     * 删除指定用户信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Integer id) {
        log.info("删除的用户Id为:{}", id);
        iUmsAdminService.removeById(id);
        return AjaxResult.success("删除成功");
    }

    /**
     * 用户注册
     *
     * @param umsAdmin
     * @return
     */
    @PostMapping("/register")
    public AjaxResult register(@RequestBody UmsAdmin umsAdmin) {
        log.info("添加用户信息为:{}", umsAdmin.toString());
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        umsAdmin.setPassword(passwordEncoder.encode(umsAdmin.getPassword()));
        iUmsAdminService.save(umsAdmin);
        return AjaxResult.success("添加成功");
    }

    /**
     * 修改指定用户信息
     *
     * @param umsAdmin
     * @param id
     * @return
     */
    @PostMapping("/update/{id}")
    public AjaxResult update(@RequestBody UmsAdmin umsAdmin, @PathVariable Long id) {
        log.info("要修改用户信息的id为:{}", id);
        umsAdmin.setId(id);
        iUmsAdminService.updateById(umsAdmin);
        return AjaxResult.success("修改成功");
    }

    /**
     * 根据指定用户修改密码
     *
     * @param updateAdminPasswordParamVo
     * @return
     */
    @PostMapping("/updatePassword")
    public AjaxResult updatePassword(@RequestBody UpdateAdminPasswordParamVo updateAdminPasswordParamVo) {
        log.info("修改的用户名为:{},新密码:{},旧密码:{}",
                updateAdminPasswordParamVo.getUsername(),
                updateAdminPasswordParamVo.getNewPassword(),
                updateAdminPasswordParamVo.getOldPassword());
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        updateAdminPasswordParamVo.setOldPassword(passwordEncoder.encode(updateAdminPasswordParamVo.getOldPassword()));
        updateAdminPasswordParamVo.setNewPassword(passwordEncoder.encode(updateAdminPasswordParamVo.getNewPassword()));
        iUmsAdminService.updateByUsername(updateAdminPasswordParamVo);
        return AjaxResult.success("修改成功");
    }

    /**
     * 根据用户名或姓名分页获取用户列表
     *
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list(@RequestParam(value = "keyword", required = false) String keyword,
                           @RequestParam(value = "pageNum", required = false) Integer pageNum,
                           @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        PageInfo pageInfo = iUmsAdminService.getPage(keyword, pageNum, pageSize);
        return AjaxResult.success(pageInfo);
    }

    /**
     * 登出功能
     *
     * @return
     */
    @PostMapping("/logout")
    public AjaxResult logout() {
        return AjaxResult.success("退出成功");
    }

    /**
     * 给用户分配角色
     *
     * @param
     * @param
     * @return
     */
    @PostMapping("/role/update")
    public AjaxResult roleUpdate(@RequestBody UmsAdminRoleRelationVo umsAdminRoleRelationVo) {
        log.info("用户id:{},修改角色编号为:{}",
                umsAdminRoleRelationVo.getAdminId(),
                umsAdminRoleRelationVo.getRoleIds().toString());
        iUmsAdminService.roleUpdate(umsAdminRoleRelationVo);
        return AjaxResult.success("分配成功");
    }

    /**
     * 获取指定用户的角色
     *
     * @param adminId
     * @return
     */
    @GetMapping("/role/{adminId}")
    public AjaxResult role(@PathVariable Long adminId) {
        log.info("查询用户关联角色传过来Id:{}", adminId);
        List<UmsRole> umsRoles = iUmsAdminService.getRoleByAdminId(adminId);
        return AjaxResult.success(umsRoles);
    }

    /**
     * 修改指定用户信息
     *
     * @return
     */
    @PostMapping("/updateStatus/{id}")
    public AjaxResult updateById(@PathVariable Long id, @RequestParam("status") Integer status) {
        log.info("id为{}的用户修改状态为{}", id, status);
        LambdaUpdateWrapper<UmsAdmin> luw = new LambdaUpdateWrapper<>();
        luw.eq(id != null, UmsAdmin::getId, id)
                .set(UmsAdmin::getStatus, status);
        iUmsAdminService.update(luw);
        return AjaxResult.success("修改成功");
    }

    /**
     * 获取指定用户信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult getUser(@PathVariable Long id) {
        log.info("查询id为:{}用户的信息", id);
        LambdaQueryWrapper<UmsAdmin> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UmsAdmin::getId, id);
        UmsAdmin user = iUmsAdminService.getOne(lqw);
        return AjaxResult.success(user);
    }


}
